﻿using System;
using System.Collections;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Connection;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model
{
    /// <summary>
    /// Default implementation of CounterRows interface. This is just a generic list
    /// of CounterRow objects.
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="N"></typeparam>
    public class CounterRowsImpl<K, N> : CounterRows<K, N>
    {
        public Dictionary<K, CounterRow<K, N>> Rows
        {
            get;
            protected set;
        }
        public CounterRowsImpl(Dictionary<K, List<CounterColumn>> thriftRet, Serializer<N> nameSerializer)
        {
            if (thriftRet == null || nameSerializer == null)
            {
                throw new NullReferenceException("Thrift counter column and Name serializer cannot be null");
            }
            Rows = new Dictionary<K, CounterRow<K, N>>(thriftRet.Count);
            foreach (K key in thriftRet.Keys)
            {
                Rows[key] = new CounterRowImpl<K, N>(key, thriftRet[key], nameSerializer);
            }
        }
        public CounterRow<K, N> GetByKey(K key)
        {
            return Rows[key];
        }
        public int Count
        {
            get
            {
                return Rows.Count;
            }
        }
        public System.Collections.Generic.IEnumerator<CounterRow<K,N>> GetEnumerator()
        {
            List<CounterRow<K, N>> list = new List<CounterRow<K, N>>(Rows.Values);
            return list.GetEnumerator();
        }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
        public override string ToString()
        {
            return "Rows(" + Rows + ")";
        }
    }
}
